package com.jingbian.cppccbackend.mapper.admin;

import com.jingbian.cppccbackend.entity.Admins;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jingbian.cppccbackend.entity.Users;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员信息表 Mapper 接口
 * </p>
 *
 * @author yezi
 * @since 2025-02-09
 */
@Mapper
public interface AdminsMapper extends BaseMapper<Admins> {
    @Select("select * from admins where account=#{account}")
    Admins findByAccount(String account);

    // 获取用户列表（可选择是否只查询已通过的用户）
    List<Users> getUsers(String searchText, String startDate, String endDate, String sortType,
                         String status, String identity, String region, String affiliation, String ethnicity, String gender, String auditStatus);

    @Select("select * from admins where aid=#{aid}")
    Admins findById(Integer aid);

    @Select("select * from users where uid=#{uid}")
    Users getUsersDetails(Integer uid);

    // 改用 XML 中定义的动态 SQL
    void updateStatus(Integer uid, String status);

    //修改用户信息
    @Update("update users set uname=#{uname}, unit=#{unit},gender=#{gender},identity=#{identity},ethnicity=#{ethnicity}," +
            "phone=#{phone}, affiliation=#{affiliation},birth_date=#{birthDate},region=#{region}, reserved_info=#{reservedInfo},address=#{address},postal_code=#{postalCode}" +
            " where uid=#{uid}")
    int updateUser(Users user);
    
    //删除用户
    @Delete("delete from users where uid=#{uid}")
    int deleteUser(Integer uid);


    //重置密码
    @Update("update users set password=#{newPassword} where uid=#{uid}")
    int resetPassword(Integer uid, String newPassword);

    // 获取用户总数
    @Select("SELECT COUNT(*) FROM users")
    Integer getTotalUsers();

    // 获取昨日新增用户数
    @Select("SELECT COUNT(*) FROM users WHERE DATE(registration_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)")
    Integer getNewUsersYesterday();

    // 获取待审核用户数量
    @Select("SELECT COUNT(*) FROM users WHERE status = '未处理'")
    int getPendingAuditCount();

    // 获取用户界别分布
    List<Map<String, Object>> getUserAffiliationDistribution();

    // 获取用户注册趋势 - 支持日期范围
    List<Map<String, Object>> getUserRegistrationTrend(String startDate, String endDate);

    @Select("select name from propositions_departments where id=#{departmentId}")
    String getDepartmentName(String departmentId);

    @Select("select name from public_departments where id=#{departmentId}")
    String getDepartmentName2(String departmentId);

    // 批量更新用户分数
    @Update("update users set score = #{score} where uid = #{uid}")
    int updateUserScore(Integer uid, Double score);

    // 清零所有用户分数
    @Update("update users set score = 0")
    int clearAllScores();
}
